package com.tio.showcase.showcaseintf;

import com.tio.showcase.common.Const;
import com.tio.showcase.common.ShowcasePacket;
import com.tio.showcase.packet.BaseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.utils.json.Json;

/**
 * Author      : liyuemin
 * Time        : 2017/12/15 10:51
 * Description :
 */
public abstract class AbsShowcaseBsHandler<T extends BaseBody> implements  ShowcaseBsHandlerIntef{
    private static Logger log = LoggerFactory.getLogger(AbsShowcaseBsHandler.class);
    public abstract  Class<T> bodyClass();

    /**
    * @Description : 接收到的消息分配给对应的处理类处理。
    * @param       : [packet, context]
    * @return      : java.lang.Object
    * @author      : liyuemin
    * @date        : 2017/12/15
    */
    public Object handler(ShowcasePacket packet, ChannelContext context) throws Exception {
        String jsonstr = null;
        T bsBody = null;
        if(packet.getBody()!= null){
            jsonstr = new String(packet.getBody(), Const.CHARSET);
            bsBody = Json.toBean(jsonstr,bodyClass());
        }
        return handle(packet,bsBody,context);
    }

    public abstract  Object handle(ShowcasePacket showcasePacket,T bsBody,ChannelContext channelContext)throws  Exception;
}
